$(function () {
    layui.use('layer',function () {
        var layer = layui.layer;
    });

    layui.use('form', function(){
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });

    $.post("/supplier/findAll",null,function (info) {
        let str = "";
        for(let i=0; i<info.length;i++){
            str += `<option>${info[i].name}</option>`;
        }
        $("#supplierName").html(str);
    });

    $.post("/user/getUser",null,function (info) {
        var obj = JSON.parse(info);
        $("#purchaseName").html(obj.realName);
    })
});

//添加商品
function add() {
    layer.open({
        title:"添加采购商品",//标题
        type:1,//弹出层的类别,1表示页面层
        shadeClose:true,//点击遮罩，关闭对话框
        // skin:'layui-layer-rim',//加上边框
        area:['500px','600px'],//宽高
        anim:4,
        content:$("#addDiv")
    });
}

//移除商品
function del(id) {
    $.post("/shop/purchaseDel","goodsId="+id,function (shopList) {
        show(shopList);
    });
}

//修改数量
function update(id,num) {
    $.post("/shop/purchaseUpdate","goodsId="+id+"&number="+num,function (shopList) {
        show(shopList);
    });
}

//根据ID查找商品
function findById(id) {
    $.post("/Goods/findById","goodsId="+id,function (obj) {
        if(obj != null && obj != ""){
            $("#name").val(obj.name).attr("disabled",true);
            $("#factory").val(obj.factory).attr("disabled",true);
            $("#type").val(obj.type).attr("disabled",true);
            $("#purchasePrice").val(obj.purchasePrice).attr("disabled",true);
            $("#salePrice").val(obj.salePrice).attr("disabled",true);
            $("#image").attr("src","/html/img/"+obj.image);
            $("#image").val(obj.image);
            $("#file").attr("disabled",true);
        }else {
            $("#name").val(obj.name).attr("disabled",false);
            $("#factory").val(obj.factory).attr("disabled",false);
            $("#type").val("电脑产品").attr("disabled",false);
            $("#purchasePrice").val(obj.purchasePrice).attr("disabled",false);
            $("#salePrice").val(obj.salePrice).attr("disabled",false);
            $("#image").attr("src","")
            $("#file").attr("disabled",false);
        }
    });
}

//入库
function inputStore() {
    $.post("/purchase/submit","supplierName="+$("#supplierName").val()+"&purchaseName="+$("#purchaseName").html(),function (info) {
        if(info == 'ok'){
            show(null);
            layer.open({
                title:"入库",//标题
                type:1,//弹出层的类别,1表示页面层
                shadeClose:true,//点击遮罩，关闭对话框
                // skin:'layui-layer-rim',//加上边框
                area:['420px','240px'],//宽高
                anim:4,
                content:$("#inputStoreDiv")
            });
        }else {
            layer.tips("<span>请添加商品</span>", "#inError");
            return ;
        }
    });
}

//展示商品
function addGoods() {
    if(($("#id").val() == "" && $("#id").val().length==0)){
        layer.tips("<span>请输入商品编号</span>", "#idError");
        return ;
    }
    if(($("#name").val() == "" && $("#name").val().length==0)){
        layer.tips("<span>请输入商品名称</span>", "#nameError");
        return ;
    }
    if(($("#factory").val() == "" && $("#factory").val().length==0)){
        layer.tips("<span>请输入厂家</span>", "#factoryError");
        return ;
    }
    if(($("#purchasePrice").val() == "" && $("#purchasePrice").val().length==0)){
        layer.tips("<span>请输入采购价</span>", "#purchasePriceError");
        return ;
    }
    if(($("#salePrice").val() == "" && $("#salePrice").val().length==0)){
        layer.tips("<span>请输入销售价</span>", "#salePriceError");
        return ;
    }
    if(($("#num").val() == "" && $("#num").val().length==0)){
        layer.tips("<span>请输入采购数量</span>", "#numError");
        return ;
    }
    if(($("#image").attr("src") == "" && $("#image").attr("src").length==0)){
        layer.tips("<span>请选择图片</span>", "#fileError");
        return ;
    }
    $("#name").attr("disabled",false);
    $("#factory").attr("disabled",false);
    $("#type").attr("disabled",false);
    $("#purchasePrice").attr("disabled",false);
    $("#salePrice").attr("disabled",false);
    $("#file").attr("disabled",false);
    
    
    $.post("/shop/purchaseAdd","goodsBean="+$("#addForm").serialize()+"&num="+$("#num").val()+"&id="+$("#id").val()+"&image="+$("#image").val(),function (shopList) {
        show(shopList);
    });
    $("#reset").click();
    $("#num").val(null);
    $("#image").attr("src","");
    clear();
}

function show(shopList) {
    if(shopList != null){
        var str = "";
        for(var i=0; i<shopList.length; i++){
            var obj = shopList[i].goods;
            str += `<tr><td>${obj.name}</td><td>${obj.factory}</td><td>${obj.purchasePrice}</td><td>${obj.salePrice}</td>
            <td><input type="text" value="${shopList[i].num}" onblur="update(${obj.id},this.value)"></td><td><a class="layui-btn layui-btn-normal" href="javascript:del(${obj.id})">删除商品</a></td></tr>`;
        }
        $("#tableData").html(str);
    }else {
        $("#tableData").html("");
    }
}



function clear() {
    layer.closeAll();
}

function showImg(path) {
    $("#image").attr("src","/html/img/"+path.substring(path.lastIndexOf("\\")+1));
    $("#image").val(path.substring(path.lastIndexOf("\\")+1));
}